package com.castle.fortress.admin.goods.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.castle.fortress.admin.core.constants.GlobalConstants;
import com.castle.fortress.admin.goods.entity.DeliveryMethodEntity;
import com.castle.fortress.admin.goods.dto.DeliveryMethodDto;
import com.castle.fortress.admin.goods.service.DeliveryMethodService;
import com.castle.fortress.common.utils.ConvertUtil;
import com.castle.fortress.common.entity.RespBody;
import com.castle.fortress.common.exception.BizException;
import com.castle.fortress.common.respcode.GlobalRespCode;
import com.castle.fortress.common.enums.OperationTypeEnum;
import com.castle.fortress.admin.core.annotation.CastleLog;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import springfox.documentation.annotations.ApiIgnore;
import org.apache.logging.log4j.util.Strings;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.stereotype.Controller;
import com.castle.fortress.admin.utils.ExcelUtils;
import javax.servlet.http.HttpServletResponse;
import com.castle.fortress.common.entity.DynamicExcelEntity;
import java.util.List;

/**
 * 配送方式 控制器
 *
 * @author whc
 * @since 2022-12-09
 */
@Api(tags="配送方式管理控制器")
@Controller
public class DeliveryMethodController {
    @Autowired
    private DeliveryMethodService deliveryMethodService;

    /**
     * 配送方式的分页展示
     * @param deliveryMethodDto 配送方式实体类
     * @param current 当前页
     * @param size  每页记录数
     * @return
     */
    @CastleLog(operLocation="配送方式",operType = OperationTypeEnum.QUERY)
    @ApiOperation("配送方式分页展示")
    @GetMapping("/goods/deliveryMethod/page")
    @ResponseBody
    @RequiresPermissions("goods:deliveryMethod:pageList")
    public RespBody<IPage<DeliveryMethodDto>> pageDeliveryMethod(DeliveryMethodDto deliveryMethodDto, @RequestParam(required = false) Integer current, @RequestParam(required = false)Integer size){
        Integer pageIndex= current==null? GlobalConstants.DEFAULT_PAGE_INDEX:current;
        Integer pageSize= size==null? GlobalConstants.DEFAULT_PAGE_SIZE:size;
        Page<DeliveryMethodDto> page = new Page(pageIndex, pageSize);
        IPage<DeliveryMethodDto> pages = deliveryMethodService.pageDeliveryMethodExtends(page, deliveryMethodDto);

        return RespBody.data(pages);
    }

    /**
     * 配送方式的列表展示
     * @param deliveryMethodDto 配送方式实体类
     * @return
     */
    @CastleLog(operLocation="配送方式",operType = OperationTypeEnum.QUERY)
    @ApiOperation("配送方式列表展示")
    @GetMapping("/goods/deliveryMethod/list")
    @ResponseBody
    public RespBody<List<DeliveryMethodDto>> listDeliveryMethod(DeliveryMethodDto deliveryMethodDto){
        List<DeliveryMethodDto> list = deliveryMethodService.listDeliveryMethod(deliveryMethodDto);
        return RespBody.data(list);
    }

    /**
     * 配送方式保存
     * @param deliveryMethodDto 配送方式实体类
     * @return
     */
    @CastleLog(operLocation="配送方式",operType = OperationTypeEnum.INSERT)
    @ApiOperation("配送方式保存")
    @PostMapping("/goods/deliveryMethod/save")
    @ResponseBody
    @RequiresPermissions("goods:deliveryMethod:save")
    public RespBody<String> saveDeliveryMethod(@RequestBody DeliveryMethodDto deliveryMethodDto){
        if(deliveryMethodDto == null ){
            throw new BizException(GlobalRespCode.PARAM_MISSED);
        }
        DeliveryMethodEntity deliveryMethodEntity = ConvertUtil.transformObj(deliveryMethodDto,DeliveryMethodEntity.class);
        if(deliveryMethodService.save(deliveryMethodEntity)){
            return RespBody.data("保存成功");
        }else{
            return RespBody.fail(GlobalRespCode.OPERATE_ERROR);
        }
    }

    /**
     * 配送方式编辑
     * @param deliveryMethodDto 配送方式实体类
     * @return
     */
    @CastleLog(operLocation="配送方式",operType = OperationTypeEnum.UPDATE)
    @ApiOperation("配送方式编辑")
    @PostMapping("/goods/deliveryMethod/edit")
    @ResponseBody
    @RequiresPermissions("goods:deliveryMethod:edit")
    public RespBody<String> updateDeliveryMethod(@RequestBody DeliveryMethodDto deliveryMethodDto){
        if(deliveryMethodDto == null || deliveryMethodDto.getId() == null || deliveryMethodDto.getId().equals(0L)){
            throw new BizException(GlobalRespCode.PARAM_MISSED);
        }
        DeliveryMethodEntity deliveryMethodEntity = ConvertUtil.transformObj(deliveryMethodDto,DeliveryMethodEntity.class);
        if(deliveryMethodService.updateById(deliveryMethodEntity)){
            return RespBody.data("保存成功");
        }else{
            return RespBody.fail(GlobalRespCode.OPERATE_ERROR);
        }
    }

    /**
     * 配送方式删除
     * @param id
     * @return
     */
    @CastleLog(operLocation="配送方式",operType = OperationTypeEnum.DELETE)
    @ApiOperation("配送方式删除")
    @PostMapping("/goods/deliveryMethod/delete")
    @ResponseBody
    @RequiresPermissions("goods:deliveryMethod:delete")
    public RespBody<String> deleteDeliveryMethod(@RequestParam Long id){
        if(id == null ){
            throw new BizException(GlobalRespCode.PARAM_MISSED);
        }
        if(deliveryMethodService.removeById(id)) {
            return RespBody.data("保存成功");
        }else{
            return RespBody.fail(GlobalRespCode.OPERATE_ERROR);
        }
    }


    /**
     * 配送方式批量删除
     * @param ids
     * @return
     */
    @CastleLog(operLocation="配送方式",operType = OperationTypeEnum.DELETE)
    @ApiOperation("配送方式批量删除")
    @PostMapping("/goods/deliveryMethod/deleteBatch")
    @ResponseBody
    @RequiresPermissions("goods:deliveryMethod:deleteBatch")
    public RespBody<String> deleteDeliveryMethodBatch(@RequestBody List<Long> ids){
        if(ids == null || ids.size()<1){
            throw new BizException(GlobalRespCode.PARAM_MISSED);
        }
        if(deliveryMethodService.removeByIds(ids)) {
            return RespBody.data("保存成功");
        }else{
            return RespBody.fail(GlobalRespCode.OPERATE_ERROR);
        }
    }

    /**
     * 配送方式详情
     * @param id 配送方式id
     * @return
     */
    @CastleLog(operLocation="配送方式",operType = OperationTypeEnum.QUERY)
    @ApiOperation("配送方式详情")
    @GetMapping("/goods/deliveryMethod/info")
    @ResponseBody
    @RequiresPermissions("goods:deliveryMethod:info")
    public RespBody<DeliveryMethodDto> infoDeliveryMethod(@RequestParam Long id){
        if(id == null){
            throw new BizException(GlobalRespCode.PARAM_MISSED);
        }
        DeliveryMethodDto deliveryMethodDto =  deliveryMethodService.getByIdExtends(id);

        return RespBody.data(deliveryMethodDto);
    }

	/**
     * 动态表头导出 依据展示的字段导出对应报表
     * @param dynamicExcelEntity
     * @param response
     * @throws Exception
     */
    @CastleLog(operLocation="配送方式",operType = OperationTypeEnum.EXPORT)
	@PostMapping("/goods/deliveryMethod/exportDynamic")
	@ApiOperation("动态表头导出，依据展示的字段导出对应报表")
	public void exportDynamic(@RequestBody DynamicExcelEntity<DeliveryMethodDto> dynamicExcelEntity, HttpServletResponse response) throws Exception {
		List<DeliveryMethodDto> list = deliveryMethodService.listDeliveryMethod(dynamicExcelEntity.getDto());
		//字典、枚举、接口、json常量等转义后的列表数据 根据实际情况初始化该对象，null为list数据直接导出
		List<List<Object>> dataList = null;
		/**
        * 根据实际情况初始化dataList,可参照 com.castle.fortress.admin.system.controller.TmpDemoController类中方法：exportDynamic
        */
		ExcelUtils.exportDynamic(response,dynamicExcelEntity.getFileName()+".xlsx",null,list,dynamicExcelEntity.getHeaderList(),dataList);
	}


}
